package servlet;

import dao.Db;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginAdmin extends GenericServlet {

    @Override
    public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
        //接受用户请求，访问业务逻辑层，获取表中所有数据，并且渲染成表格
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //收参
        String user = req.getParameter("user");
        String password = req.getParameter("password");

        //访问数据访问层
        Db db = new Db();
        boolean admin=false;
        try {
            admin=db.isAdmin(user,password);
            System.out.println("admin = " + admin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (admin){
            //服务器转换，转换到showList
            req.getRequestDispatcher("worker").forward(req,resp);
        }else {
            HttpServletResponse resp1 = (HttpServletResponse) resp;
            resp1.sendRedirect("error.html");
        }
    }
}
